//
//  ShoppingCartRecommendsView.m
//  Yanwei
//
//  Created by weijunjie on 2018/12/6.
//  Copyright © 2018年 DCloud. All rights reserved.
//

#import "ShoppingCartRecommendsView.h"
#import "NullStateView.h"
#import "MallHomeContentDataModel.h"
#import "MallHomeTwoGoodsCollectionCell.h"

@interface ShoppingCartRecommendsView()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *iLickView;
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) NullStateView *nullDataStateView;
@property (nonatomic, strong) NullStateView *nullCollectDataStateView;
@property (nonatomic, strong) NSArray *data;
@property (nonatomic, assign) CGFloat goodsTotalH;
@end

@implementation ShoppingCartRecommendsView

- (UIView *)iLickView {
    if (!_iLickView) {
        _iLickView = [[UIView alloc] init];
        _iLickView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_iLickView];
        UIImageView *iconView = [[UIImageView alloc] init];
        iconView.image = [UIImage imageNamed:@"store_like_icon"];
        _iconView = iconView;
        [_iLickView addSubview:iconView];
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.iLickView);
            make.top.equalTo(self.iLickView).offset((44 - iconView.image.size.height) * 0.5);
            make.size.mas_equalTo(iconView.image.size);
        }];
    }
    return _iLickView;
}
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        CGFloat width = 165.0 / 375.0 * ScreenWidth;
        layout.itemSize = CGSizeMake(width, [MallHomeContentDataModel twoGoodsPerLineHeight]);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
        [self addSubview:_collectionView];
        [_collectionView registerClass:[MallHomeTwoGoodsCollectionCell class] forCellWithReuseIdentifier:[MallHomeTwoGoodsCollectionCell cellClass]];
    }
    return _collectionView;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.data.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MallHomeTwoGoodsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[MallHomeTwoGoodsCollectionCell cellClass] forIndexPath:indexPath];
        GoodsModel *model = self.data[indexPath.row];
        cell.model = model;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.toGoodsDetailBlock) {
        self.toGoodsDetailBlock(self.data[indexPath.row]);
    }
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = Color244;
    }
    return self;
}


#pragma mark - 购物车内容为空
- (NullStateView *)nullDataStateView {
    if (!_nullDataStateView) {
        _nullDataStateView = [[NullStateView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 194) fromViewType:ShoppingCartEmptyType];
        [self addSubview:_nullDataStateView];
    }
    return _nullDataStateView;
}
- (NullStateView *)nullCollectDataStateView {
    if (!_nullCollectDataStateView) {
        _nullCollectDataStateView = [[NullStateView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 194) fromViewType:ShopCollectionListEmptyType];
        [self addSubview:_nullCollectDataStateView];
    }
    return _nullCollectDataStateView;
}
- (CGFloat)goodsTotalH {
    return  ([MallHomeContentDataModel twoGoodsPerLineHeight] + 10) * ceil(self.data.count * 0.5);
}
- (void)setData:(NSArray *)data hasCartData:(BOOL)hasCartData {
    self.data = data;
    if (!hasCartData) { // 没有商品
        self.nullDataStateView.frame = CGRectMake(0, 15, ScreenWidth, 179);
        self.nullDataStateView.hidden = NO;
    } else {
        self.nullDataStateView.frame = CGRectZero;
        self.nullDataStateView.hidden = YES;
    }
    [self calculateSubViewsWithTopMargin:self.nullDataStateView.height];
    self.iconView.image = [UIImage imageNamed:hasCartData ? @"store_like_data_icon" :  @"store_like_icon"];
}
- (void)setData:(NSArray *)data hasCollectData:(BOOL)hasCollectData {
    self.data = data;
    if (!hasCollectData) { // 没有商品
        self.nullCollectDataStateView.frame = CGRectMake(0, 15, ScreenWidth, 179);
        self.nullCollectDataStateView.hidden = NO;
    } else {
        self.nullCollectDataStateView.frame = CGRectZero;
        self.nullCollectDataStateView.hidden = YES;
    }
    [self calculateSubViewsWithTopMargin:self.nullCollectDataStateView.height];
}
- (void)calculateSubViewsWithTopMargin:(CGFloat)topMargin {
    if (topMargin == 0) {
        topMargin = 10;
    }
    self.iLickView.frame = CGRectMake(0, topMargin, ScreenWidth, 49);
    self.collectionView.frame = CGRectMake(0, CGRectGetMaxY(self.iLickView.frame), ScreenWidth, self.goodsTotalH);
    [self.collectionView reloadData];
    self.frame = CGRectMake(0, 0, ScreenWidth, CGRectGetMaxY(self.collectionView.frame));
}

@end
